package com.edgclub.edg.uc;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;


import com.edgclub.edg.R;
import com.edgclub.edg.adapter.ChannelsSeleteAdapter;
import com.edgclub.edg.model.TopicChannel;

import java.nio.channels.Channel;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author 陈坤彬
 *
 */
public class ChannelsSelectDialog extends Dialog implements
		View.OnClickListener {

	private ListView listView;

	private ChannelListViewListener listenner;

	private ArrayList<TopicChannel> topicChannels;

	private ChannelsSeleteAdapter adapter;

	private Context mContext;

	public ChannelsSelectDialog(Context context, ArrayList<TopicChannel> topicChannels1) {
		super(context, R.style.MyDialogStyleBottom);
		mContext = context;
		if (topicChannels == null){
			topicChannels = new ArrayList<>();
		}
		topicChannels.clear();
		for(TopicChannel channel : topicChannels1){
			if (channel.getState() == 0){
				topicChannels.add(channel);
			}
		}
		TopicChannel channel = new TopicChannel();
		channel.setTitle("取消");
		topicChannels.add(channel);
	}
	public void setData( ArrayList<TopicChannel> models){
		this.topicChannels = models;
	}

	public ChannelListViewListener getListenner() {
		return listenner;
	}

	public void setListenner(ChannelListViewListener listenner) {
		this.listenner = listenner;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_gz_channelselect_dialog);
		listView = (ListView) findViewById(R.id.list);
		adapter = new ChannelsSeleteAdapter(mContext);
		adapter.setTopicChannels(topicChannels);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if ("取消".equals(topicChannels.get(position).getTitle())){
					dismiss();
				} else {
					dismiss();
					listenner.onItemClick(position,topicChannels.get(position));
				}
			}
		});
		// 设置大小
		Display disp = getWindow().getWindowManager().getDefaultDisplay();
		WindowManager.LayoutParams lp = getWindow().getAttributes();

		int hh = (int) mContext.getResources().getDimension(R.dimen.size_40);

		lp.y = hh; // 30是屏幕底部toolbar的高度；
		lp.width = (int) disp.getWidth();
		lp.gravity = Gravity.BOTTOM;
		getWindow().setAttributes(lp);

	}

	public interface ChannelListViewListener {
		public void onItemClick(int position, TopicChannel data);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		dismiss();
		return false;
	}

	@Override
	public void onClick(View arg0) {
		dismiss();
	}
}
